/******************************************************************************************************
Copyright (C) 2011 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/** Take blind region list from active spectrum, print some info and append a new blind region
*/
function blindRegionTest()
{
	var nmrSpc = nmr.activeSpectrum()
	if(nmrSpc.isValid()) {
//get blind regions array
		var blindRegionsArray = nmrSpc.blindRegions;
		print(blindRegionsArray);
//remove the last one
		if(blindRegionsArray.lenght!=0)
			blindRegionsArray.pop();
//create a new one
		var newBlindRegion = new BlindRegion(5.5,6.0);
		newBlindRegion.name = "My Region"
//print some info.
		print(newBlindRegion.f1.from)
		print(newBlindRegion.f1.to)
		print(newBlindRegion)
//append to the array
		blindRegionsArray.push(newBlindRegion);
		print(blindRegionsArray);
//json like
		var jsonString = '{"name": "Another", "f1": {"from":"2.1","to":"3"} }';
		var anotherBlindRegion = JSON.parse(jsonString)
		print(anotherBlindRegion.f1.from)
		print(JSON.stringify(anotherBlindRegion));
//apply to spectrum
		nmrSpc.auditTrail.addCommand(function() {
				nmrSpc.blindRegions = blindRegionsArray;
				nmrSpc.update();
				mainWindow.activeWindow().update();
			}, null, null, "Set Blind Regions", { "data": JSON.stringify(blindRegionsArray) });
	}
}

/** Takes the blind region list from the active spectrum and saves it to a file that can be opended from the GUI
*/
function blindRegionExport()
{
	var nmrSpc = nmr.activeSpectrum()
	if(nmrSpc.isValid()) {
		var blindRegionsArray = nmrSpc.blindRegions;
		if(blindRegionsArray.lenght!=0) {
//array to xml string
			var xmlString = BlindRegion.exportList(blindRegionsArray);
			print(xmlString);
//get file name
			var filename = FileDialog.getSaveFileName ("*.xml;;*.*", "Save To File", Dir.home())
			var file = new File(filename);
			if(file.open(File.WriteOnly)) {
//write to file
				var strm = new TextStream(file);
				strm.write(xmlString);
				strm.flush();
			}
		}
	}
}

/** Reads blind region list from file and applies it to the active spectrum
*/
function blindRegionImport()
{
	var nmrSpc = nmr.activeSpectrum()
	if(nmrSpc.isValid()) {
//get filename
		var filename =  FileDialog.getOpenFileName ("*.xml;;*.*", "Import File", Dir.home())
		var file = new File(filename);
		if(file.open(File.ReadOnly)) {
//read the xml string
			var strm = new TextStream(file);
			var xmlString = strm.readAll(xmlString);
//create the list
			var blindRegionsArray = BlindRegion.importList(xmlString);
			print(blindRegionsArray);
//apply to spectrum
			nmrSpc.blindRegions = blindRegionsArray;
			nmrSpc.update();
			mainWindow.activeWindow().update();
		}
	}
}
